//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.ActivityImportWizards
{
    using System.Windows;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System;

    /// <summary>
    /// 
    /// </summary>
    public partial class WSActivityPreview : IActivityWizardScreens
    {
        WSWizardPresenter WSPresenter = null;

        public WSActivityPreview(WSWizardPresenter presenter)
        {
            this.InitializeComponent();

            this.DataContext = presenter.WSModel;
            WSPresenter = presenter;

            // Get the current cursor and show busy cursor.
            System.Windows.Input.Cursor currentCursor = this.Cursor;
            this.Cursor = System.Windows.Input.Cursors.Wait;

            // Load the activity preview.
            ITridentUIElement element = presenter.LoadActivityPreview();

            // clear the preview holder and add the preview.
            this.previewHolder.Child = null;
            this.previewHolder.Child = element as UIElement;

            // Set the cursor back
            this.Cursor = currentCursor;
        }

        /// <summary>
        /// Called when [click next].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickNext(object sender, RoutedEventArgs e)
        {
            if (null != NextButtonEventHandler)
            {
                if (WSPresenter.WSModel.IsCopyActivityCodeEnabled &&
                    string.IsNullOrEmpty(WSPresenter.WSModel.OutputBinariesPath))
                {
                    TridentMessageBox.ShowTridentWarningMessageBox(
                        TridentResourceManager.GetString("OutputBinaryPath"));

                    return;
                }

                // Generate the actvitiy code and assembly.
                WSPresenter.WSModel.IsActivityGeneratedSuccessful = WSPresenter.GenerateActivity();
                NextButtonEventHandler(sender, new ActivityImportEventArgs((new WSImportSummary(WSPresenter))));
            }
        }

        /// <summary>
        /// Called when [click cancel].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickCancel(object sender, RoutedEventArgs e)
        {
            if (null != CancelButtonEventHandler)
            {
                if (TridentMessageBox.ShowTridentConfirmationYesNoMessageBox(
                    TridentResourceManager.GetString("WizardCloseMessage")) == System.Windows.MessageBoxResult.Yes)

                    CancelButtonEventHandler(sender , e);
            }
        }

        /// <summary>
        /// Called when [click back].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickBack(object sender, RoutedEventArgs e)
        {
            if (null != BackButtonEventHandler)
            {
                BackButtonEventHandler(sender, e);
            }
        }

        private void Browse_Click(object sender, RoutedEventArgs e)
        {
            // Open a Folder to copy activity codes and assemblies.
            string selectedOutPath = WSWizardPresenter.FolderBrowse();

            if (!string.IsNullOrEmpty(selectedOutPath))
            {
                WSPresenter.WSModel.OutputBinariesPath = selectedOutPath;
            }
        }

        #region IActivityWizardScreens Members

        /// <summary>
        /// Occurs when [Next button handler].
        /// </summary>
        public event EventHandler<ActivityImportEventArgs> NextButtonEventHandler;

        /// <summary>
        /// Occurs when [Back button handler].
        /// </summary>
        public event EventHandler BackButtonEventHandler;

        /// <summary>
        /// Occurs when [Cancel button handler].
        /// </summary>
        public event EventHandler CancelButtonEventHandler;

        /// <summary>
        /// Handles move request.
        /// </summary>
        public event EventHandler OnWindowMoveEventHandler;

        #endregion

        /// <summary>
        /// Called when mouse left button down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.OnWindowMoveEventHandler != null)
            {
                this.OnWindowMoveEventHandler(sender, e);
            }
        }
    }
}